package  
{
	/**
	 * ...
	 * @author weoweo
	 */
	import com.Astart.AcorrsLineGrid;
	import com.Astart.Grid;
	import com.Astart.Node;
	import com.Astart.Astart;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getTimer;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLLoaderDataFormat;
	
	[SWF(width="600",height="600")]
	public class AstartExam extends Sprite
	{
		
		
		private var _canvas:BitmapData;
		private var _canvasBitmap:Bitmap;
		
		private var _grid:Grid;
		private var _showTime:TextField;
		private var _randomModul:Boolean = true;
		private var _acorrs:AcorrsLineGrid;
		private var _numRow:int=200;
		private var _numCol:int=200;
		
		[Embed(source="../_File/nijia2/ground/texture/mappic.jpg")]
		private static var pic:Class;
		private static var _mapData:BitmapData=new BitmapData(200, 200, false, 0xffffff) ;//在这初始定义地图尺寸(500*500);

		
		public function AstartExam() 
		{
			_drawmap()
			
			
			
		}
		private  function _drawmap():void{
			_mapData.draw(Bitmap(new pic()).bitmapData);
			datas=[];
			var len:int=(_numRow * _numCol) / 100;
			for(var i:int=0;i<200;i++){
				for(var j:int=0;j<200;j++){
					if(_mapData.getPixel(i,j)<0xcc0000){
						datas.push([i, j])
					}
				}
			}
			init();

		}
		private function loadDatas():void {
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.addEventListener(Event.COMPLETE, loaded);
			loader.load(new URLRequest("nodeData.txt"));
		}
		private var arString:String;
		private var datas:Array;
		private function loaded(e:Event):void {
			arString = String(e.currentTarget.data);
			datas = arString.split("|");
			
			var len:int = datas.length;
			for (var i:int = 0; i < len; i++ ) {
				
				var str:String = datas[i] as String;
				var arr:Array = str.split("^");
				datas[i] = arr;
			}
			init();
		}
		private function random():void {
			
			datas = [];
			var len:int = (_numRow * _numCol) / 5;
			for (var i:int = 0; i < len; i++ ) {
				
				var xn:int = Math.random() * _numCol;
				var yn:int = Math.random() * _numRow;
				datas.push([xn,yn]);
			}
			trace("------------------------------------------");
			//var mm:Array = new Array(1, 1);
			//nodes = mm;
			init();
		}
		
		private var _shape:Shape;
		private function init(e:Event=null):void {
			
			if (stage.stageWidth < 1 || stage.stageHeight < 1) {
				
				if (!stage.hasEventListener(Event.RESIZE)) stage.addEventListener(Event.RESIZE, init);
				return;
			}
			
			if (stage.hasEventListener(Event.RESIZE)) stage.removeEventListener(Event.RESIZE, init);
			
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			_canvas = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xffffff);
			_canvasBitmap = new Bitmap(_canvas);
			addChild(_canvasBitmap);
			addChild(_shape = new Shape());
			
			_grid = new Grid(stage.stageWidth, stage.stageHeight,_numRow,_numCol);
			_grid.canvasGrid(_canvas);
			_acorrs = new AcorrsLineGrid(_grid);
			var nodes:Array = _grid.nodes;
			
			trace("------------------------------------------");
			for each(var a:Array in datas) {
				var yn:int = int(a[1]); var xn:int = int(a[0]);
				Node(nodes[yn][xn]).walkAble=false
			}
			
			
			
			_grid.nodes[0][0].walkAble = true;
			
			_grid.drawUnWalks();
			
		
			stage.addEventListener(MouseEvent.CLICK, clickHandle);
			_showTime = new TextField();
			_showTime.x = 10; _showTime.y = 30;
			_showTime.wordWrap = true;
			_showTime.autoSize = "left";
			_showTime.border = true;
			_showTime.borderColor = 0xffffff;
			_showTime.background = true;
			_showTime.backgroundColor = 0xFFFFFF;
			var textFormat:TextFormat = new TextFormat("Aria", 16, 0xFF0000,true);
			_showTime.defaultTextFormat = textFormat;
			addChild(_showTime);
			
		}
		
		
		private function clickHandle(e:MouseEvent):void {
			
			var posx:Number = _canvasBitmap.mouseX;
			var posy:Number = _canvasBitmap.mouseY;
			var hitNode:Node = _grid.getNodeByXY(posx, posy) as Node;
			if (!hitNode||!hitNode.walkAble) return;
				_grid.startNode = _grid.endNode?_grid.endNode:_grid.nodes[0][0];
				_grid.endNode = hitNode;
				findPath();
				return;
			
			if (_grid.startNode == null) {
				 _grid.startNode = _grid.getNodeByXY(posx,posy) as Node;
				 return;
			}
			
			if (_grid.endNode == null) {
				var hitNode:Node=_grid.getNodeByXY(posx,posy) as Node;
				if (hitNode != _grid.startNode) _grid.endNode = hitNode;
				findPath();
				return;
			}
			
			_grid.changeNodeStaByXY(posx, posy);
			findPath();
			
		}
		
		private var _path:Array;
		private function findPath():void {
			_grid.canvasGrid(_canvas);
			_grid.nodesRest();
			var startTime:uint = getTimer();
			Astart.ProfectPath = true;
			_path = Astart.findPath(this._grid);
			//trace("we had visted node : " + Astart._vistedNum + " Times .");
			//trace("dureution  " + (getTimer() - startTime));
			
			//_grid.drawRectByArr(Astart._visited, 0x999999);
			_grid.drawUnWalks();
			if (_path == null|| _path.length==0) {
				trace("NO path!!");
				return;
			}
			
			//_grid.drawRectByArr(Astart.cool, 0x00ff00);
			var psArr:Array = _acorrs.getPsnode(_path);
			_grid.drawRectByArr(_path, int(0xFF00FF));
			_shape.graphics.clear();
			_grid.drawPathLine(_shape.graphics, _path, 0xFF0000);
			_grid.drawPathLine(_shape.graphics, psArr, 0x00ff00);
			var useTime:uint = getTimer()-startTime;
			_showTime.text = "共访问:"+ Astart._vistedNum+ "次网格,用时: "+useTime;
			
			return;
			graphics.clear();
			graphics.lineStyle(1, 0xFF0000, 1);
			var shift:Node = Node(_path.shift());
			var sx:Number = shift.x * _grid.nodeWidth;
			var sy:Number = shift.y * _grid.nodeWidth;
			graphics.moveTo(sx, sy);
			if (_path) addEventListener(Event.EXIT_FRAME, drawPath);
		}
		private function drawPath(e:Event):void {
			
			var shift:Node = Node(_path.shift());
			
			if (shift == null) {
				removeEventListener(Event.EXIT_FRAME, drawPath);
				return;
			}
			var sx:Number = shift.x * _grid.nodeWidth;
			var sy:Number = shift.y * _grid.nodeWidth;
			graphics.lineTo(sx, sy);
		}
		
		
		
		private function updataHandle(e:Event):void {
			//this._densy = _panel.densy;
			//this._isEightDerection = _panel.isEightDerection;
			//this._size = _panel.size;
			
		}

	}

}

